
package com.jims.master.modules.doctor.service;

import com.google.common.collect.Lists;
import com.jims.master.common.config.Global;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.dao.ClinicOrdersCostDao;
import com.jims.master.modules.doctor.dao.ClinicOrdersDao;
import com.jims.master.modules.doctor.dao.PrescriptionApplyDao;
import com.jims.master.modules.doctor.dao.PrescriptionItemDao;
import com.jims.master.modules.doctor.entity.*;
import com.jims.master.modules.oper.dao.PriceListDao;
import com.jims.master.modules.oper.entity.PriceList;
import com.jims.master.modules.operation.entity.OperationApply;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 门诊医嘱Service
 * @author CTQ
 * @version 2017-03-13
 */
@Service
@Transactional(readOnly = true)
public class ClinicOrdersService extends CrudService<ClinicOrdersDao, ClinicOrders> {
	@Autowired
	private PrescriptionApplyDao prescriptionApplyDao;
	@Autowired
	private PrescriptionItemDao prescriptionItemDao;
	@Autowired
	private ClinicOrdersCostDao clinicOrdersCostDao;
	@Autowired
	private PriceListDao priceListDao;
	@Autowired
	private PrescriptionItemService prescriptionItemService;
	@Autowired
	private ClinicMasterService clinicMasterService;



	public Page<ClinicOrders> findPage(Page<ClinicOrders> page, ClinicOrders clinicOrders) {
		return super.findPage(page, clinicOrders);
	}

	/**
	 * 门诊收费查询 收费项目主记录
	 * @param page
	 * @param clinicOrders
	 * @return
	 * @author 赵宁
	 */
	public Page<ClinicOrders> findOrdersPage(Page<ClinicOrders> page, ClinicOrders clinicOrders) {
		clinicOrders.setPage(page);
		page.setList(dao.findClinicOrders(clinicOrders));
		return page;
	}

	@Transactional(readOnly = false)
	public int savePrescription(ClinicOrders clinicOrders,String clinicId) {
		int num = 0;
		if(StringUtils.isNotBlank(clinicOrders.getApplyId())){
			PrescriptionApply prescriptionApply = prescriptionApplyDao.get(clinicOrders.getApplyId());
			if (prescriptionApply!=null){

				PrescriptionItem item = new PrescriptionItem();
				item.setApplyId(clinicOrders.getApplyId());
				List<PrescriptionItem> list = prescriptionItemDao.findListByApplyId(item);
				List<ClinicOrdersCost> costList = Lists.newArrayList();
				if (list!=null&&list.size()>0){
					for (PrescriptionItem pi :list) {
						ClinicOrdersCost clinicOrdersCost = new ClinicOrdersCost();
						/**1.判断是否是子处方，由于子处方用药途径与主处方用药途径只收一次途径项目费用**/
						if (StringUtils.isBlank(pi.getParent().getId())){
							//costList.add(handleCost(pi,clinicOrdersCost));
							prescriptionItemService.handleAdministration(pi,prescriptionApply);
						}
						costList.add(handleCost(pi,clinicOrdersCost));
					}
				}
				/**2.删除orders与orders_cost*/
				if (StringUtils.isNotBlank(prescriptionApply.getOrdersId())){
					clinicOrdersCostDao.delByOrderId(prescriptionApply.getOrdersId());
					dao.delete(new ClinicOrders(prescriptionApply.getOrdersId()));
				}


				ClinicOrders orders = handleOrders(prescriptionApply);
				orders.updateOrgId();
				ClinicMaster clinicMaster=clinicMasterService.get(clinicId);
				orders.setApplyDept(clinicMaster.getDeptId());
				orders.preInsert();
				num = dao.insert(orders);
                /**
                 * 更改处方申请表状态
                 */
                prescriptionApply.setPrescStatus(Global.YES);
                prescriptionApply.setOrdersId(orders.getId());
                prescriptionApply.preUpdate();
                prescriptionApplyDao.update(prescriptionApply);
				saveOrdersCosts(orders,costList);
			}
		}
		return num;
	}

	/**
	 * 手术申请收费
	 * @param operationApply
	 * @return
	 */
	@Transactional(readOnly = false)
	public String saveOperationApplyCost(OperationApply operationApply){
		int num=0;
		ClinicOrders clinicOrders=new ClinicOrders();
		clinicOrders.setClinicId(operationApply.getClinicId());
		clinicOrders.setPatientId(operationApply.getPatientId());
		clinicOrders.setItemClass("F");
		clinicOrders.setChargeFlag("0");
		clinicOrders.setImplementUser(operationApply.getDoctorUser());
		clinicOrders.setImplementDept(operationApply.getOperationDeptId());
		clinicOrders.setApplyId(operationApply.getId());
		clinicOrders.setOrgId(operationApply.getOrgId());
		clinicOrders.preInsert();
		dao.insert(clinicOrders);
		return clinicOrders.getId();
	}
	/**
	 * 处理医嘱明细
	 * @param prescriptionItem
	 * @param clinicOrdersCost
	 * @return
	 * @author CTQ
	 */
	private ClinicOrdersCost handleCost(PrescriptionItem prescriptionItem,ClinicOrdersCost clinicOrdersCost){
		PriceList priceList = priceListDao.get(prescriptionItem.getPriceId());
		if (priceList!=null){
			clinicOrdersCost.setPriceId(priceList.getId());
			clinicOrdersCost.setSubjCode(priceList.getSubjCode());//会计科目
			clinicOrdersCost.setCoefficient(Double.valueOf(1));//收费系数
			clinicOrdersCost.setClassOnReckoning(priceList.getClassOnReckoning());//核算项目科目
			clinicOrdersCost.setItemClass(priceList.getItemClass());
			clinicOrdersCost.setItemName(prescriptionItem.getDrugName());
			clinicOrdersCost.setItemCode(prescriptionItem.getDrugCode());
			clinicOrdersCost.setNum(prescriptionItem.getNum());
			clinicOrdersCost.setSpec(prescriptionItem.getSpec());
			clinicOrdersCost.setUnit(prescriptionItem.getPackageUnit());
		}
		return clinicOrdersCost;
	}

	/**
	 * 处理医嘱信息
	 * @param prescriptionApply
	 * @return
	 * @author CTQ
	 */
	private ClinicOrders handleOrders(PrescriptionApply prescriptionApply){
		ClinicOrders orders = new ClinicOrders();
		orders.setApplyId(prescriptionApply.getId());
		orders.setApplyNo(prescriptionApply.getApplyOn());
		orders.setChargeFlag("0");//是否收费
		orders.setClinicId(prescriptionApply.getClinicId());
		orders.setImplementDept(prescriptionApply.getImplementDept());
//		orders.setImplementUser();
		orders.setItemClass(prescriptionApply.getItemClass());
		orders.setPatientId(prescriptionApply.getPatientId());
//		orders.setRcptNo();
		orders.setIsApply(Global.YES);//是否是申请单
		return orders;
	}

	/**
	 * 保存医嘱明细数据
	 * @param orders
	 * @return
	 * @author CTQ
	 */
	private void saveOrdersCosts(ClinicOrders orders,List<ClinicOrdersCost> list){
		if (list!=null&&list.size()>0){
			for (ClinicOrdersCost cost : list ) {
				if (cost!=null){
					cost.setPatientId(orders.getPatientId());
					cost.setClinicId(orders.getClinicId());
					cost.setOrdersId(orders.getId());
					cost.updateOrgId();
					cost.preInsert();
					clinicOrdersCostDao.insert(cost);
				}
			}
		}
	}

	/**
	 * 门诊退费
	 * @param id
	 * @param chargeFlag
	 * @return
	 */
	@Transactional(readOnly = false)
     public int refundIng(String id ,String chargeFlag){
         ClinicOrders clinicOrders =new ClinicOrders();
         clinicOrders.setId(id);
         clinicOrders.setChargeFlag(chargeFlag);
         int num = dao.refundIng(clinicOrders) ;
         return num;
     }

    /**
     * 查找clinicmaster信息
     * @param ordersId
     * @return
     */
    public ClinicMaster findClinicMaster(String ordersId) {
        return dao.findClinicMaster(ordersId);
    }
}